package com.dy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.common.R;
import com.dy.common.RedisConstonts;
import com.dy.dto.SetmealDto;
import com.dy.entity.Dish;
import com.dy.entity.Setmeal;
import com.dy.entity.SetmealDish;
import com.dy.mapper.SetmealMapper;
import com.dy.service.SetmealDishService;
import com.dy.service.SetmealService;
import com.dy.utils.Oss_chengdu_blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;


    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private StringRedisTemplate strRedis;

    /**
     * @Author dy
     * @Description: 新增套餐  / 和套餐绑定的菜品
     */
    @Transactional
    public R<String> saveSetmeal(SetmealDto setmealDto) {
        //新增套餐
        this.save(setmealDto);

        //新增套餐绑定的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(setmealDish ->{
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

        //setmeal：         套餐对应的分类ID
        String key = RedisConstonts.SETMEAL_LIST + setmealDto.getCategoryId() + "_" + "1";
        //新增完套餐，删除套餐对应的分类缓存
        strRedis.delete(key);
        return R.success("新增成功.");
    }


    //获取 套餐分页查询数据
    public List<SetmealDto> getPageData(Integer page, Integer pageSize, String name) {
        List<SetmealDto> list = setmealMapper.getPageData(page,pageSize,name);
        return list;
    }

    //获取数据总数
    public Long getTotal(String name) {
        Long toatl = setmealMapper.getTotal(name);
        return toatl;
    }


    //删除套餐 + 对应套餐绑定的菜品信息
    @Transactional
    public R<String> delSetmealAndDish(List<Long> ids) {
        //过滤 删除套餐是否 还在启售
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);
        if(count > 0){
            return R.error("套餐还在启售中，请先停售套餐。(*^_^*)");
        }
        //删除成功，清除对应套餐分类缓存
        LambdaQueryWrapper<Setmeal> wapper = new LambdaQueryWrapper();
        wapper.select(Setmeal::getCategoryId);
        wapper.in(Setmeal::getId,ids);
        wapper.groupBy(Setmeal::getCategoryId);
        //获取修改状态的每个菜品对应的分类ID
        List<Setmeal> list = this.list(wapper);
        for (Setmeal setmeal : list) {
            //新增完套餐，删除套餐对应的分类缓存
            strRedis.delete(RedisConstonts.SETMEAL_LIST + setmeal.getCategoryId() + "_" + "1");
        }

        //获取要删除的 套餐图片路径
        List<String> fileName = setmealMapper.getFileName(ids);
        //删除对应OSS 保存的图片
        Oss_chengdu_blog.deleteFile(fileName);

        //删除 套餐
        this.removeByIds(ids);
        //删除 套餐对应的绑定菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);


        return R.success("删除成功");
    }

    //修改套餐 状态
    @Transactional
    public R<String> updateStatus(int status, List<Long> ids) {
        List<Setmeal> collect = ids.stream().map(id -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            return setmeal;
        }).collect(Collectors.toList());

        boolean b = this.updateBatchById(collect);
        if(BooleanUtil.isTrue(b)){
            //修改成功，清除对应套餐分类缓存
            LambdaQueryWrapper<Setmeal> wapper = new LambdaQueryWrapper();
            wapper.select(Setmeal::getCategoryId);
            wapper.in(Setmeal::getId,ids);
            wapper.groupBy(Setmeal::getCategoryId);
            //获取修改状态的每个菜品对应的分类ID
            List<Setmeal> list = this.list(wapper);
            for (Setmeal setmeal : list) {
                //删除套餐对应的分类缓存
                strRedis.delete(RedisConstonts.SETMEAL_LIST + setmeal.getCategoryId() + "_" + "1");
            }

            return R.success("修改成功");
        }
        return R.error("修改失败，请重试。");
    }


    //根据id查询对应 套餐数据
    public R<SetmealDto> getDtoById(Long id) {
        //查询套餐数据
        Setmeal setmeal = this.getById(id);
        SetmealDto dto = new SetmealDto();
        BeanUtil.copyProperties(setmeal,dto);

        //查询套餐对应的 菜品数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        dto.setSetmealDishes(list);
        //返回数据
        return R.success(dto);
    }


    @Transactional
    //修改套餐数据
    public R<String> updateSetmeal(SetmealDto setmealDto) {
        //修改套餐
        this.updateById(setmealDto);

        //删除原套餐对应的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //新增新修改的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

        //setmeal：         套餐对应的分类ID
        String key = RedisConstonts.SETMEAL_LIST + setmealDto.getCategoryId() + "_" + "1";
        //修改完套餐，删除套餐对应的分类缓存
        strRedis.delete(key);
        return R.success("修改成功");
    }
}
